package com.iteye.trafficRecorder;

import java.util.List;
import java.util.UUID;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegistFragment extends Fragment  implements ResponseListener {
	
	private EditText txtPhoneNumber;
	
	private EditText txtCarNumber;
	
	private EditText txtUserName;
	
	private EditText txtDeviceType;
	
	private User user;
	
	private MainActivity _this;
	
	private DatabaseHelper dbHelper;
	
	private Setting setting;
	
	private boolean isStarted;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		isStarted = false;
		View view = inflater.inflate(R.layout.layout_regist, container);
	 	return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		_this = (MainActivity)getActivity();
		setting = new Setting(_this);
		dbHelper = _this.getdb();
        
        txtPhoneNumber = (EditText)_this.findViewById(R.id.txt_phone_number);
        txtCarNumber = (EditText)_this.findViewById(R.id.txt_car_number);
        txtUserName = (EditText)_this.findViewById(R.id.txt_user_name);
        txtDeviceType = (EditText)_this.findViewById(R.id.txt_device_type);
        
        user = dbHelper.getUser();
        if (user != null) {
        	txtCarNumber.setText(user.getCarNumber());
        	txtUserName.setText(user.getUserName());
        	txtPhoneNumber.setText(user.getPhoneNumber());
        	txtDeviceType.setText(user.getDeviceType());
        } else {
        	DeviceInfo deviceInfo = new DeviceInfo(_this);
        	txtPhoneNumber.setText(deviceInfo.getPhoneNumber());
        	txtDeviceType.setText(deviceInfo.getDeviceType());
        }
        
        Button btnRegist = (Button)_this.findViewById(R.id.btn_regist);
        btnRegist.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				String guid = UUID.randomUUID().toString();
				User newUser = initUser();
				if (user != null) {
					newUser.setGuid(user.getGuid());
					dbHelper.updateUser(newUser);
				} else {
					newUser.setGuid(guid);
					dbHelper.insertUser(newUser);
				}
				List<NameValuePair> params = WebService.initParams(WebService.dataType_mobile_info, guid, newUser.toXml());
				try {
					HttpClientHelper.asyncPostTextRequest(_this, setting.getWebServiceUrl(), params, RegistFragment.this);
				} catch (Exception e) {
					Log.e("MainActivity", "Error in submit regist info!", e);
				}
			}
		});
        
        final Button btnService = (Button)_this.findViewById(R.id.btn_service);
        btnService.setVisibility(View.GONE);
        btnService.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent service = new Intent(_this, LocationService.class);
				isStarted = !isStarted;
				if (isStarted) {
					btnService.setText("Stop Service");
			        _this.startService(service);
				} else {
					btnService.setText("Start Service");
			        _this.stopService(service);
				}
				
			}
        });
	}
	
	public void onResponseSuccess(HttpResponse response, Object result, Object myLocation) {
		Toast.makeText(_this, R.string.regist_success, Toast.LENGTH_SHORT).show();
        Intent service = new Intent(_this, LocationService.class);
        _this.startService(service);
	}

	public void onResponseFailed(HttpResponse response, Throwable error, Object myLocation) {
		Toast.makeText(_this, R.string.regist_failed, Toast.LENGTH_SHORT).show();
	}
	
	private User initUser() {
		User user = new User();
		user.setPhoneNumber(txtPhoneNumber.getText().toString());
		user.setCarNumber(txtCarNumber.getText().toString());
		user.setUserName(txtUserName.getText().toString());
		user.setDeviceType(txtDeviceType.getText().toString());
		return user;
	}
	
}
